/*
 * @Author Shunzi
 * @Date 2021-07-12 12:22:51
 * @LastEditTime 2021-07-18 14:45:08
 * @LastEditors Shunzi
 * @Description
 * @FilePath /BFFNodeServer/app.js
 */
import express from "express";
import swig from "swig";
import log4js from "log4js";
import formidable from "express-formidable";
import routes from "./controllers/index";
import config from "./config/config";
import ErrorHandler from "./middlewares/ErrorHandler";

const app = express();

app.use(express.json()); // 解析 json
app.use(express.urlencoded({ extended: true })); // 解析 application/x-www-form-urlencoded
// app.use(formidable()); // 解析 formdata

app.use(express.static("static"));
// 配置 Swig
app.set("view engine", "html");
app.engine("html", swig.renderFile);

// 配置错误日志
log4js.configure({
  appenders: { globalError: { type: "file", filename: "./logs/error.log" } },
  categories: { default: { appenders: ["globalError"], level: "error" } },
});

// 配置路由
app.use(routes);

// 容错
const logger = log4js.getLogger("globalError");
ErrorHandler.error(app, logger);

app.listen(config.port, function () {
  console.log(`app is started,listening ${config.port} port`);
});
